<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/12 0012
 * Time: 10:49
 */

namespace app\author\controller;


use think\Controller;

class Index extends Allow
{
    public function index(){
        $member_id = session('author_member_id');
        $member = db("member")->where('id',$member_id)->find();
        $member_id = session('author_member_id');
        $book = db("book")->where('m_id',$member_id)->select();
        $this->assign([
            'member' => $member,
            'book' => $book
        ]);
        return view("index");
    }
    public function index_menu(){
            $member_id = session('author_member_id');
            $book = db("book")->where('m_id',$member_id)->select();
            $data = [
                'book' => $book
            ];
            return $data;
    }
    public function welcome(){
        $member_id = session('author_member_id');
        return "欢迎来到 作者后台。请开始展示你的才华";
    }
    public function info(){
        if(request()->post()){
            $param = request()->param();
            $member = db("member")->where("id",$param['id'])->find();
            if(isset($param['image'])){
                $image = $member['head_img'];
            }else{
                $file = request()->file('image');
                $info=$file->move( './static/images');
                $filename=str_replace("\\","/",$info->getSaveName());
                $image = '/static/images/'.$filename;
            }
            $data = [
                'nickname' => $param['nickname'],
                'head_img' => $image,
                'pen_name' => $param['pen_name'],
            ];
            db("book")->where('m_id',$param['id'])->update(['author' => $data['pen_name']]);
            $res = db("member")->where("id",$param['id'])->update($data);
            if($res){
                return ['state' => 200,'msg' => 'SUCCESS'];
            }else{
                return ['state' => -1,'msg' => 'ERROR'];
            }
        }
        $id = input("id");
        $member = db("member")->where("id",$id)->find();
//        dump($member);die;
        $this->assign([
            'member' => $member
        ]);
        return view("info");
    }
}